package org.flower.web.servlet;

import com.jspsmart.upload.*;
import org.flower.bean.Carousel;
import org.flower.dao.CarouselDao;
import org.flower.dao.CommodityDetailsDao;
import org.flower.dao.impl.CarouselDaoImpl;
import org.flower.dao.impl.CommodityDetailsDaoImpl;
import org.flower.entity.CommodityDetails;
import org.flower.service.CarouselService;
import org.flower.service.CommodityDetailsService;
import org.flower.service.impl.CarouselServiceImpl;
import org.flower.service.impl.CommodityDetailsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 旋转木马servlet
 *
 * @author Aizen
 * @date 2021/12/27
 */
@WebServlet(urlPatterns="/manager/CarouselServlet.shtml")
public class CarouselServlet extends HttpServlet {


	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		CarouselService cs = new CarouselServiceImpl();
		List<Carousel> carouselList = cs.findAllCarousel();

		//查询商品
		CommodityDetailsDao cd = new CommodityDetailsDaoImpl();
		List<CommodityDetails> commodityDetailsList = cd.getCommodityDetails();

		request.setAttribute("commodityDetailsList",commodityDetailsList);
		request.setAttribute("carouselList",carouselList);
		request.getRequestDispatcher("carousel.jsp").forward(request,response);
	}


	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//1.实例化对象
		SmartUpload smartUpload = new SmartUpload();
		//2.设置编码
		smartUpload.setCharset("utf-8");
		//3.初始化
		smartUpload.initialize(this,request,response);
		//4.文件上传
		try {
			smartUpload.upload();
		} catch (SmartUploadException e) {
			e.printStackTrace();
		}
		//5.获取req对象
		Request req = smartUpload.getRequest();

		//如果是同步过来的，获取文件域中的
		String image = "";
		Files files = smartUpload.getFiles();
		for (int i = 0; i < files.getCount(); i++) {
			File file = files.getFile(i);
			try {
				file.saveAs("image/carousel/"+file.getFileName());
				image = "image/carousel/"+file.getFileName();
			} catch (SmartUploadException e) {
				e.printStackTrace();
			}
		}

		org.flower.entity.Carousel carousel = new org.flower.entity.Carousel();
		carousel.setImage(image);

		CarouselDao cd = new CarouselDaoImpl();
		boolean b = cd.addCarousel(carousel);
		if(b){
			response.getWriter().write(
					"<script type=\"text/javascript\">alert('添加成功！');setInterval(\"\",10);parent.location.href='" +
							request.getContextPath()
							+ "/manager/CarouselServlet.shtml'</script>");
			response.getWriter().flush();
			response.getWriter().close();
		}else{
			response.getWriter().write(
					"<script type=\"text/javascript\">alert('添加失败...');setInterval(\"\",10);parent.location.href='" +
							request.getContextPath()
							+ "/manager/CarouselServlet.shtml'</script>");
			response.getWriter().flush();
			response.getWriter().close();
		}

	}

}
